package cn.source.system.service.impl;

import cn.source.common.core.redis.RedisCache;
import cn.source.common.utils.uuid.CodeUtil;
import cn.source.system.service.CaptchaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class CaptchaServiceImpl implements CaptchaService {
    private static final Logger logger = LoggerFactory.getLogger(CaptchaServiceImpl.class);

    @Autowired
    private RedisCache redisCache;


    @Override
    public String getCode(String userName) {
        String code = "";

        if(redisCache.getCacheObject(userName) == null){
            code = CodeUtil.getCapthCode();
            // 将验证码限制参数放到缓存里 10分钟过期，10分钟后才能再次发短信
            redisCache.setCacheObject(userName, code , 10,TimeUnit.MINUTES);

            logger.info(code);
        }else{
            code = redisCache.getCacheObject(userName);
        }

        return code;
    }
}
